import { CENTEREVENTS } from '@amcax/base';
import { nextmeshEventCenter } from '@app-cad/environments/nextmesh/common/events/EventCenter';
import Base from '../cmd-common/base';
import { nmAppLog } from '@app-nextmesh/common/utils';
import { dataManager } from '@app-cad/module';
import { documentV2 } from '@app-nextmesh/domain';
import { renderModels } from '@app-nextmesh/utils/renderModel';

export default class CmdInteractor extends Base {
  setPoint: any;

  constructor(setPoint) {
    super();
    this.setPoint = setPoint;
    this.setMode(['face', 'line', 'point']);
    this.registerEvents();
    // this.renderPoint();
  }

  renderPoint = async () => {
    const ids = [];
    Array.from(dataManager.shapesDataMap).forEach((item) => {
      item[1]['category'] === 'SHAPE' && ids.push(item[0]);
    });
    const renderData = await documentV2.getRenderData(ids);
    renderModels(renderData);
  };
  registerEvents = () => {
    nextmeshEventCenter()
      .setThreshold(8)
      .on(CENTEREVENTS.POINTERMOVE, this.onMouseMove)
      .on(CENTEREVENTS.POINTERDOWN, this.onMouseUp)
      .addExit(this.destroy)
      .on(CENTEREVENTS.KEYUP, this.onKeyUp);
  };
  unregisterEvents = () => {
    nextmeshEventCenter()
      .off(CENTEREVENTS.POINTERMOVE, this.onMouseMove)
      .off(CENTEREVENTS.POINTERDOWN, this.onMouseUp)
      .off(CENTEREVENTS.KEYUP, this.onKeyUp)
      .setDefault();
  };
  onMouseMove = (e) => {
    this.mouseMove(e, false);
  };
  onMouseUp = (e) => {
    const mouseup = this.mouseUp(e);
    this.setPoint(mouseup);
  };
  onEnter = () => {
    nmAppLog('enter');
  };
  destroy = () => {
    this.unregisterEvents();
    this.removePointArrow();
    this.reset();
  };

  addExit(exitFunc: () => void) {
    this.exitFunc = exitFunc;
  }
}
